package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.widget.ImageView;

import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.CdbInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.exception.ERROR;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.ILamplightAc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/4 16:00
 * 邮箱：zhanghuaiha@gmail.com
 */

public class LamplightPresenter extends BasePresenter<ILamplightAc> {

    private ImageView flicker;
    private ImageView open;
    private ImageView close;
    private String versionName;
    private String udid;
    private String eid;

    public LamplightPresenter(BaseActivity context) {
        super(context);
    }

    public void loadDate(String alias){
        open = getView().getOpen();
        flicker = getView().getFlicker();
        close = getView().getClose();

        String time = System.currentTimeMillis() + "";
        versionName = ObtainVersion.getVersionName(mContext);
        udid = App.SP.getString("udid", "");
        eid = App.SP.getString("id", "");
        String encry = ObtainEncry.getCdb_info("2", time, versionName, NetWorkCodeInfo.GETCDB_INFO, udid, eid, alias);
        RetrofitFactory.getmInstanse().mApi.getCdb_info("2",time, versionName, NetWorkCodeInfo.GETCDB_INFO, udid, eid,alias,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<CdbInfo>>() {
                    @Override
                    public void success(BaseResponseEntity<CdbInfo> cdbInfoBaseResponseEntity) {
                        if (cdbInfoBaseResponseEntity.code == 200) {
                            CdbInfo data = cdbInfoBaseResponseEntity.data;
                            CdbInfo.ChargingBean charging = data.getCharging();
                            String green_light_status = charging.getGreen_light_status();
                            if (green_light_status.equals("0")) {
                                close.setSelected(true);
                            }else if (green_light_status.equals("1")) {
                                flicker.setSelected(true);
                            }else {
                                open.setSelected(true);
                            }
                        }else {
                            ToastUtil.showToast(cdbInfoBaseResponseEntity.message);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }

    public void changeState(String alias,String status){
        String time = System.currentTimeMillis() + "";
        String encry = ObtainEncry.getState("2", time, versionName, NetWorkCodeInfo.GREEN_LIGHTING, udid, alias, eid, status);
        RetrofitFactory.getmInstanse().mApi.getGreen_lighting("2",time,versionName,NetWorkCodeInfo.GREEN_LIGHTING,udid,alias,eid,status,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<String>>() {
                    @Override
                    public void success(BaseResponseEntity<String> stringBaseResponseEntity) {
                        if (stringBaseResponseEntity.code == 200) {
                           // ToastUtil.showToast(stringBaseResponseEntity.data);
                        }else {
                            ToastUtil.showToast(stringBaseResponseEntity.message);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }
}
